<template>
  <div>
    <el-dialog
      v-model="dialogVisible"
      width="80%"
      align-center
      :show-close="false"
      destroy-on-close
      @close="handleClose"
      draggable
    >
      <template #header="{ close }">
        <div class="dialog-header flex justify-between items-center">
          <div>发送通知</div>
          <el-icon class="el-icon--left cursor-pointer" @click="close"
            ><CircleCloseFilled color="#fff"
          /></el-icon>
        </div>
      </template>
      <div class="bg-white my-[10px] py-[10px]">
        <BaseHead>邮件信息</BaseHead>
        <el-form
          :model="editForm"
          :rules="rules"
          label-width="130px"
          ref="editFormRef"
          class="mt-[10px]"
        >
          <el-form-item label="标题" prop="title">
            <el-input placeholder="标题" clearable v-model="editForm.title" style="width: 40%">
            </el-input>
          </el-form-item>
          <el-form-item label="接收人" prop="receivers">
            <span v-for="item in editForm.receivers" :key="item.receiverNumber"
              >{{ item.receiverName }}
              <i :style="{ fontStyle: 'normal', color: !item.receiverNumber ? 'red' : '' }"
                >[{{ item.receiverNumber || "未填写邮箱" }}]</i
              >；</span
            >
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <div class="flex-1">
              <Editor v-model="editForm.content" :min-height="200" />
            </div>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <div class="operate-btns">
            <el-button type="primary" @click="save">发送</el-button>
            <el-button type="primary" @click="dialogVisible = false">关闭</el-button>
          </div>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import BaseHead from "@/views/project/components/BaseHead.vue";
import { sendMessage } from "@/api/project/xj/xmps/expert";
import { ElMessage } from "element-plus";
const dialogVisible = ref(false);
const editFormRef = ref();
//基本信息表单
const editForm = reactive({
  title: "",
  content: "",
  receivers: [],
});
const rules = reactive({
  title: [
    {
      required: true,
      message: "标题必填",
    },
  ],
  content: [
    {
      required: true,
      message: "内容必填",
    },
  ],
});
//发送
const send = (persons) => {
  dialogVisible.value = true;
  editForm.receivers = persons;
};
defineExpose({ send });
const save = () => {
  try {
    editFormRef.value?.validate(async (valid) => {
      if (valid) {
        //检查专家邮箱是否有为空的
        let nullFlag = false;
        for (let i = 0; i < editForm.receivers.length; i++) {
          if (!editForm.receivers[i].receiverNumber) {
            nullFlag = true;
            break;
          }
        }
        if (nullFlag) {
          ElMessage({
            message: "接收人中有未填写邮箱的，请到专家信息中完善邮箱信息！",
            type: "error",
          });
          return;
        }
        const res = await sendMessage(editForm);
        if (res.code === 200) {
          ElMessage({
            message: "操作成功！",
            type: "success",
          });
          dialogVisible.value = false;
        }
      }
    });
  } catch (error) {
    console.log(error);
  }
};
//关闭弹窗
const handleClose = () => {
  editFormRef.value.resetFields();
};
</script>

<style scoped lang="scss">
@import url("@/views/project/index.scss");
</style>
